package com.wondersgroup.insurance.dnuilearn.recyclerview.swipe.extend;

import android.graphics.Canvas;
import android.support.v7.widget.RecyclerView;

import com.wondersgroup.insurance.dnuilearn.recyclerview.swipe.ItemTouchHelperAdapterCallback;
import com.wondersgroup.insurance.dnuilearn.recyclerview.swipe.QQMessageAdapter1;

/**
 * Created by Administrator on 2017/6/28.
 */

public class MessageTouchItemCallBack2 extends ItemTouchHelper.Callback {

    ItemTouchHelperAdapterCallback callback;

    public MessageTouchItemCallBack2() {
    }

    public MessageTouchItemCallBack2(ItemTouchHelperAdapterCallback callback) {
        this.callback = callback;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        int dragFlags = ItemTouchHelper.UP|ItemTouchHelper.DOWN;
        int swipeFlags = ItemTouchHelper.LEFT;
        return makeMovementFlags(dragFlags, swipeFlags);
    }

    @Override
    public boolean isLongPressDragEnabled() {
        return true;
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
        return true;
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        if (callback != null)callback.onItemMove(viewHolder.getAdapterPosition(),target.getAdapterPosition());
        return callback != null ;
    }
    QQMessageAdapter1.MyViewHolder preViewHolder;
    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
//        if (callback != null)  callback.onItemSwipe(viewHolder.getAdapterPosition());

    }

    @Override
    public boolean canDropOver(RecyclerView recyclerView, RecyclerView.ViewHolder current, RecyclerView.ViewHolder target) {
        return super.canDropOver(recyclerView, current, target);
    }

    @Override
    public float getSwipeThreshold(RecyclerView.ViewHolder viewHolder) {
        return super.getSwipeThreshold(viewHolder);
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        super.onSelectedChanged(viewHolder, actionState);
    }

    @Override
    public void onMoved(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int fromPos, RecyclerView.ViewHolder target, int toPos, int x, int y) {
        super.onMoved(recyclerView, viewHolder, fromPos, target, toPos, x, y);
    }
    @Override
    public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        if (dY !=0 && dX ==0 ){
            super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        }

        if (viewHolder instanceof QQMessageAdapter1.MyViewHolder){
            final QQMessageAdapter1.MyViewHolder holder = (QQMessageAdapter1.MyViewHolder) viewHolder;
//            ViewCompat.setTranslationX(holder.content,dX);


            if (dX < -holder.view_list_repo_action_container.getWidth()) {
                dX =- holder.view_list_repo_action_container.getWidth();
            }
            holder.content.setTranslationX(dX);

        }

    }

    @Override
    public void onChildDrawOver(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        super.onChildDrawOver(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }

    @Override
    public long getAnimationDuration(RecyclerView recyclerView, int animationType, float animateDx, float animateDy) {
        return super.getAnimationDuration(recyclerView, animationType, animateDx, animateDy);
    }
}
